package org.jcnc;

import java.sql.*;

public class EmployeeManager {

    // JDBC连接参数
    private static final String URL = "jdbc:mysql://localhost:3306/employee_info";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    // 插入员工信息
    public void insertEmployee(String name, String employeeId, String gender, String department, String phone,
                               String position, String address, String account) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
            String sql = "INSERT INTO employees (name, emp_id, gender, department, phone, position, address, account) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, name);
                pstmt.setString(2, employeeId);
                pstmt.setString(3, gender);
                pstmt.setString(4, department);
                pstmt.setString(5, phone);
                pstmt.setString(6, position);
                pstmt.setString(7, address);
                pstmt.setString(8, account);

                pstmt.executeUpdate();
                System.out.println("Employee inserted successfully.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 修改员工信息（根据账号）
    public void updateEmployeeByAccount(String account, String department) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
            String sql = "UPDATE employees SET department = ? WHERE account = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, department);
                pstmt.setString(2, account);

                int rowsAffected = pstmt.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("Employee information updated successfully.");
                } else {
                    System.out.println("No employee found with the provided account.");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查找员工信息（根据账号）
    public void findEmployeeByAccount(String account) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
            String sql = "SELECT * FROM employees WHERE account = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, account);

                try (ResultSet rs = pstmt.executeQuery()) {
                    if (rs.next()) {
                        String name = rs.getString("name");
                        String gender = rs.getString("gender");
                        String department = rs.getString("department");
                        String phone = rs.getString("phone");
                        String position = rs.getString("position");
                        String address = rs.getString("address");

                        System.out.println("Account: " + account);
                        System.out.println("Name: " + name);
                        System.out.println("Gender: " + gender);
                        System.out.println("Department: " + department);
                        System.out.println("Phone: " + phone);
                        System.out.println("Position: " + position);
                        System.out.println("Address: " + address);
                    } else {
                        System.out.println("No employee found with the provided account.");
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EmployeeManager employeeManager = new EmployeeManager();

        // 插入员工信息
//        employeeManager.insertEmployee("John Doe", "1001", "Male", "IT", "1234567890",
//                "Developer", "123 Main St", "johndoe");

        // 更新员工信息（根据账号）
        employeeManager.updateEmployeeByAccount("johndoe", "Finance");

        // 查找员工信息（根据账号）
        employeeManager.findEmployeeByAccount("johndoe");
    }
}
